#ifndef KEYBINDINGS_H
#define KEYBINDINGS_H

#include <QtGui>
#include <QtSql>
#include <Maestro/database.h>

class KeyBindings {
public:
    static int upKey[2];
    static int downKey[2];
    static int leftKey[2];
    static int rightKey[2];
    static int volumeUpKey[2];
    static int volumeDownKey[2];
    static int selectKey[2];
    static int secondaryKey[2];
    static int backKey[2];
    static int homeKey[2];
    static int nowPlayingKey[2];

    static int playKey[2];
    static int ffKey[2];
    static int rwKey[2];
    static int stopKey[2];
    static int skipKey[2];
    static int previousKey[2];
    static int fullscreenKey[2];


    static bool matchesKeys(int * keys,QKeyEvent * event) {
        if(keys[0]==event->key()&&keys[1]==event->modifiers()) {
            return true;
        }
        else if (keys[0]==event->key()&&keys[1]+536870912==event->modifiers()) {
            return true;
        }
        return false;
    }

    static void storeBinding(int * binding, QSqlRecord record) {
        binding[0]=record.field("key").value().toInt();
        binding[1]=record.field("modifierKeys").value().toInt();
    }

    static void adjustForOS(int * keys) {
        if(keys[1]==Qt::NoModifier||keys[1]==Qt::KeypadModifier) {

            keys[1]=Qt::NoModifier;
            #if defined(Q_OS_MAC)
                keys[1]=Qt::KeypadModifier;
            #endif
         }
    }

    static void loadKeyBindings()  {
        QSqlQueryModel * model=Querier::getModel("SELECT * FROM Shortcuts");
        for(int i=0; i<model->rowCount(); i++) {
            QSqlRecord record=model->record(i);
            QString function=record.field("function").value().toString();
            if(function=="down") {
                storeBinding(KeyBindings::downKey, record);
                if(KeyBindings::downKey[0]==Qt::Key_Down) {
                    adjustForOS(KeyBindings::downKey);
                }
            } else if(function=="up") {
                storeBinding(KeyBindings::upKey, record);
                if(KeyBindings::upKey[0]==Qt::Key_Up) {
                    adjustForOS(KeyBindings::upKey);
                }
            } else if(function=="left") {
                storeBinding(KeyBindings::leftKey, record);
                if(KeyBindings::leftKey[0]==Qt::Key_Left) {
                    adjustForOS(KeyBindings::leftKey);
                }
            } else if(function=="right") {
                storeBinding(KeyBindings::rightKey, record);
                if(KeyBindings::rightKey[0]==Qt::Key_Right) {
                    adjustForOS(KeyBindings::rightKey);
                }
            } else if(function=="volumeDown") {
                storeBinding(KeyBindings::volumeDownKey, record);
            } else if (function=="volumeUp") {
                storeBinding(KeyBindings::volumeUpKey, record);
            } else if(function=="select") {
                storeBinding(KeyBindings::selectKey, record);
            }
            else if(function=="secondary") {
                storeBinding(KeyBindings::secondaryKey, record);
            }
            else if(function=="nowPlaying") {
                storeBinding(KeyBindings::nowPlayingKey, record);
            }
            else if(function=="back") {
                storeBinding(KeyBindings::backKey, record);
            }
            else if(function=="home") {
                storeBinding(KeyBindings::homeKey, record);
            }
            else if(function=="play") {
                storeBinding(KeyBindings::playKey, record);
            }
            else if(function=="stop") {
                storeBinding(KeyBindings::stopKey, record);
            }
            else if(function=="ff") {
                storeBinding(KeyBindings::ffKey, record);
            }
            else if(function=="rw") {
                storeBinding(KeyBindings::rwKey, record);
            }
            else if(function=="skip") {
                storeBinding(KeyBindings::skipKey, record);
            }
            else if(function=="previous") {
                storeBinding(KeyBindings::previousKey, record);
            }
            else if(function=="fullscreen") {
                storeBinding(KeyBindings::fullscreenKey, record);
            }
        }
    }

    KeyBindings();
};

#endif // KEYBINDINGS_H
